using System;
using System.Collections.Generic;
using System.Reflection;

namespace RG.Common.Configuration
{

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class PropertySettingAttribute : BaseSettingsAttribute
    {
        public override IEnumerable<ISetting> Collect(object obj, string objID, PropertyInfo pi)
        {

            var setting_name = string.IsNullOrEmpty(Name) ?
                   pi.Name :
                   Name;

            if (!string.IsNullOrEmpty(objID))
                setting_name = string.Format("{0}.{1}", objID, setting_name);

            var propertySetting = new PropertySetting(setting_name, obj, pi, DefaultValue, SerializeAs)
            {
                SaveUndefinedValue = SaveNullValue,
                ThrowOnErrorDeserializing = true,
                ThrowOnErrorLoading = true,
                ThrowOnErrorSaving = true,
                ThrowOnErrorSerializing = true
            };
            return new List<ISetting> { propertySetting };
        }
    }
}
